package com.player.thirdGroup.controller;

import com.player.thirdGroup.common.ResltEntity;
import com.player.thirdGroup.entity.User;
import com.player.thirdGroup.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class IndexController {

    @Autowired
    private UserService userService;


    @RequestMapping(value = "/userLogin",method = RequestMethod.POST)
    @ResponseBody
    public ResltEntity loginin(User user, HttpServletRequest request ){
        User u = userService.login(user);
        if(u!= null){
            //更新登录时间
            u.setUserlogindate(new Date());
            userService.update(u);
            request.getSession().setAttribute("logins",u);
            return ResltEntity.createSuccessMsg("登录成功！");
        }
        return ResltEntity.createErrorMsg("用户名或者密码错误！");
    }

    //跳转树
    @RequestMapping("/index")
    public String tree(Model model,HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("logins");
        model.addAttribute("userInfo",user);
        return "/system/menu";
    }

    //欢迎页面
    @RequestMapping("/welcome")
    public String welcome(){
        return "/system/welcome";
    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(User user, HttpServletRequest request ){
        User u = userService.login(user);
        if(u!= null){
            //更新登录时间
            u.setUserlogindate(new Date());
            userService.update(u);
            request.getSession().setAttribute("logins",u);
            System.out.println(user);
            return "menu";
        }
        return "admin";
    }
}
